package jwei.apps.zappos;

import java.util.List;

import jwei.apps.helpers.ConnectionManager;
import jwei.apps.helpers.GetMethods;
import jwei.apps.json.JsonToReview;
import jwei.apps.objects.APIConstants;
import jwei.apps.objects.Review;

import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ZapposGetReviews {

    private static DefaultHttpClient httpClient = ConnectionManager.getClient();

    public static List<Review> getReviews(long id) {
        String url = String.format(APIConstants.REVIEW, id);
        String response = GetMethods.doGetWithResponse(url, httpClient);
        if (!response.equals("")) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray reviewItems = jsonResponse.getJSONArray("reviews");
                List<Review> ret = JsonToReview.convertToReviews(reviewItems);
                return ret;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            System.out.println(ZapposGetReviews.class.getCanonicalName() + ": Did not retrieve response");
            return null;
        }
    }

}
